package com.wing.res.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.PageResult;
import com.wing.res.model.response.ResProTaskRecordSearchVo;
import com.wing.res.service.ResProBomRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 质检模板-Controller
 *
 * @Author xiayang
 **/
@Slf4j
@Validated
@Api(value = "Bom记录管理", tags = "#/res/planning/bomPlan/bomLog")
@RestController("resBomPlanProjectRecordController")
@RequestMapping("res/admin/bomPlanRecord")
public class ResBomPlanProjectRecordController {

    @Resource
    private ResProBomRecordService resProBomRecordService;

    @ApiOperation(value = "查询-Bom记录", notes = "查询-bom记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResProTaskRecordSearchVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long productTypeId
            ,Long customerId
            ,String customerProCode
            ,String productStatus
            ,String customerProFigure
            ,String givenStartTimed
    ) {
        Page<ResProTaskRecordSearchVo> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("customerId", customerId);
        map.put("customerProCode", customerProCode);
        map.put("productStatus", productStatus);
        if (StrUtil.isNotEmpty(givenStartTimed)){
            // 使用 split 方法分割字符串
            String[] dates = givenStartTimed.split(" 至 ");
            // 获取开始和结束日期
            String startDate = dates[0];
            String endDate = dates[1];
            map.put("givenStartTime", startDate);
            map.put("givenEndTime", endDate);
        }
        map.put("customerProFigure", customerProFigure);
        map.put("productTypeId", productTypeId);
        Page<ResProTaskRecordSearchVo> list = resProBomRecordService.pageList4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

}
